package com.five.wisdomEat.controller;

import com.five.wisdomEat.entity.ResponseMessage;
import com.five.wisdomEat.entity.StudentInfo;
import com.five.wisdomEat.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService service;

    @GetMapping("/getStudentList")
    public ResponseMessage selectStudentList(){
        return service.selectStudentList();
    }

    @PostMapping("/insertStudent")
    public ResponseMessage insertStudent(String sNumber, String sName, String sGender, String sPhone,
                                         String sHometown, String sNationality, String sPoliticalFace,
                                         String email, String birthDate,String homeAddress,String academy,
                                         String major,String classes,String idCardNumber) throws ParseException {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setSNumber(sNumber);
        studentInfo.setSName(sName);
        studentInfo.setSGender(sGender);
        studentInfo.setSPhone(sPhone);
        studentInfo.setSHometown(sHometown);
        studentInfo.setSNationality(sNationality);
        studentInfo.setSPoliticalFace(sPoliticalFace);
        studentInfo.setEmail(email);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(birthDate);
        studentInfo.setBirthDate(date);
        studentInfo.setHomeAddress(homeAddress);
        studentInfo.setAcademy(academy);
        studentInfo.setMajor(major);
        studentInfo.setClasses(classes);
        studentInfo.setIdCardNumber(idCardNumber);
        return service.insertStudent(studentInfo);
    }

    @PostMapping("/updateStudent")
    public ResponseMessage updateStudent(String sNumber, String sName, String sGender, String sPhone,
                                         String sHometown, String sNationality, String sPoliticalFace,
                                         String email, String birthDate,String homeAddress,String academy,
                                         String major,String classes,String idCardNumber) throws ParseException {

        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setSNumber(sNumber);
        studentInfo.setSName(sName);
        studentInfo.setSGender(sGender);
        studentInfo.setSPhone(sPhone);
        studentInfo.setSHometown(sHometown);
        studentInfo.setSNationality(sNationality);
        studentInfo.setSPoliticalFace(sPoliticalFace);
        studentInfo.setEmail(email);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(birthDate);
        studentInfo.setBirthDate(date);
        studentInfo.setHomeAddress(homeAddress);
        studentInfo.setAcademy(academy);
        studentInfo.setMajor(major);
        studentInfo.setClasses(classes);
        studentInfo.setIdCardNumber(idCardNumber);
        return service.updateStudent(studentInfo);
    }

    @PostMapping("/deleteStudent")
    public ResponseMessage deleteStudent(String sNumber){
        return service.deleteStudent(sNumber);
    }
}
